package com.luck.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.CharacterSetECI;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;

public class QRCodeGenerator {
    private final static Logger logger = LoggerFactory.getLogger(QRCodeGenerator.class);

    /**
     * 创建二维码
     * @param contents        二维码内容
     * @param centerImgPath  中间图片路径
     * @param width           宽度
     * @param height          高度
     * @return
     */
    public static byte[] creatRrCode(String contents, String centerImgPath, int width, int height) {
        String binary = null;
        Hashtable hintTypes;
        hintTypes = new Hashtable();
        hintTypes.put(EncodeHintType.CHARACTER_SET, CharacterSetECI.UTF8);
        hintTypes.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hintTypes.put(EncodeHintType.MARGIN, 0);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(
                    contents, BarcodeFormat.QR_CODE, width, height, hintTypes);
            // 1、读取文件转换为字节数组
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            BufferedImage image = toBufferedImage(bitMatrix);
            if (!StringUtils.isEmpty(centerImgPath)) {
                ClassPathResource resource = new ClassPathResource(centerImgPath);
                InputStream inputStream = resource.getInputStream();
                Image imgLogo = ImageIO.read(inputStream);
                // 插入LOGO
                int widthLogo = imgLogo.getWidth(null);
                int heightLogo = imgLogo.getHeight(null);
                Graphics2D graph = image.createGraphics();
                int x = (width - widthLogo) / 2;
                int y = (height - heightLogo) / 2;
                graph.drawImage(imgLogo, x, y, widthLogo, heightLogo, null);
                Shape shape = new RoundRectangle2D.Float(x, y, widthLogo, widthLogo, 6, 6);
                graph.setStroke(new BasicStroke(3f));
                graph.draw(shape);
                graph.dispose();
                image.flush();
            }
            ImageIO.write(image, "png", out);
            return out.toByteArray();
        } catch (Exception e) {
            logger.info("生成二维码失败: e:" + e.getMessage());
        }
        return null;
    }


    /**
     * image流数据处理
     *
     * @author ianly
     */
    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }

    public static void main(String[] args) {
        byte[] bytes = QRCodeGenerator.creatRrCode("https://blog.csdn.net/ianly123", null, 200, 200);

    }
}
